package com.example.smart.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import androidx.drawerlayout.widget.DrawerLayout
import com.example.lib.constants.JumpConstants
import com.example.lib.extend.autoInt
import com.example.lib.extend.getIntentInt
import com.example.lib.extend.gone
import com.example.lib.extend.isVisi
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.utils.ToastUtils
import com.example.lib.utils.am.ActManger
import com.example.smart.R
import com.example.smart.databinding.ActivityMainBinding
import com.example.lib.utils.statusbar.StatusBarUtils
import com.example.smart.adapter.viewpager.ViewPagerAdapter
import com.example.smart.base.ui.activity.BaseActivity
import com.example.smart.base.ui.fragment.BaseFragment
import com.example.smart.databinding.LayoutHeaderBinding
import com.example.smart.listener.OnChildScrollListener
import com.example.smart.room.bean.AnimalBean
import com.example.smart.room.factory.RoomFactory
import com.example.smart.ui.fragment.HomeFragment
import com.example.smart.ui.fragment.CourseFragment
import com.example.smart.utils.DisplayUtils
import com.example.smart.utils.DisplayUtils.type

class MainActivity : BaseActivity<ActivityMainBinding>(), OnChildScrollListener {
    private var mFragments: MutableList<BaseFragment<*>> = mutableListOf()
    private lateinit var mHeaderBinding: LayoutHeaderBinding
    private val mAnimalResponse = RoomFactory.createAnimalRepository()

    override fun layoutID() = R.layout.activity_main

    override fun afterOnGetBinding(savedInstanceState: Bundle?) {
        super.afterOnGetBinding(savedInstanceState)
        includeBinding.root.gone()
        mHeaderBinding = LayoutHeaderBinding.bind(binding.HomeLeftMenu.inflateHeaderView(R.layout.layout_header))
    }

    override fun initView() {
        StatusBarUtils.setStateAndNavigationBar(this@MainActivity, statusBarColor = com.example.lib.R.color.translate)

        mFragments = mutableListOf(HomeFragment.getInstance(), CourseFragment.getInstance())
        binding.MainPager.isUserInputEnabled = false
        binding.MainPager.adapter = ViewPagerAdapter(this@MainActivity, mFragments)
        binding.MainPager.offscreenPageLimit = mFragments.size

        binding.root.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
    }

    override fun initListener() {
        binding.MainGroup.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.MainHome -> {
                    binding.MainPager.setCurrentItem(0, false)
                }

                R.id.MainStudy -> {
                    binding.MainPager.setCurrentItem(1, false)
                }
            }
        }

        mHeaderBinding.HeaderUserPage.setOnDoubleClickListener {
            DisplayUtils.toMine(this@MainActivity)
        }

        mHeaderBinding.HeaderClose.setOnDoubleClickListener {
            binding.root.close()
        }

        mHeaderBinding.HeaderOrder.setOnDoubleClickListener {
            DisplayUtils.toDayTask(this@MainActivity)
        }

        mHeaderBinding.HeaderChat.setOnDoubleClickListener {
            DisplayUtils.toChatList(this@MainActivity)
        }

        mHeaderBinding.HeaderExit.setOnDoubleClickListener {
            DisplayUtils.toLoginChoose(this@MainActivity)
        }

        mHeaderBinding.HeaderPrivacy.setOnDoubleClickListener {
            ToastUtils.showToast("暂未开发")
        }

        mHeaderBinding.HeaderFeedback.setOnDoubleClickListener {
            ToastUtils.showToast("暂未开发")
        }

        binding.MainManage.setOnDoubleClickListener {
            DisplayUtils.toMine(this@MainActivity)
        }

        binding.MainCommunity.setOnDoubleClickListener {
            DisplayUtils.toCommunity(this@MainActivity)
        }
    }

    override fun initData() {
        mAnimalResponse.getAllData {
            if(it.isEmpty()){
                mAnimalResponse.insert(AnimalBean(
                    328,
                    "2024年10月",
                    "苏尼特",
                    "母",
                    "50",
                    "苏尼特",
                    "2024年11月",
                    "11",
                    ""
                ))

                mAnimalResponse.insert(AnimalBean(
                    123,
                    "2024年5月",
                    "苏尼特",
                    "母",
                    "50",
                    "苏尼特",
                    "2024年7月",
                    "14",
                    ""
                ))

                mAnimalResponse.insert(AnimalBean(
                    247,
                    "2024年8月",
                    "苏尼特",
                    "公",
                    "60",
                    "苏尼特",
                    "2024年9月",
                    "15",
                    ""
                ))

                mAnimalResponse.insert(AnimalBean(
                    303,
                    "2024年6月",
                    "苏尼特",
                    "母",
                    "65",
                    "苏尼特",
                    "2024年7月",
                    "0",
                    ""
                ))

                mAnimalResponse.insert(AnimalBean(
                    24,
                    "2024年2月",
                    "苏尼特",
                    "公",
                    "45",
                    "苏尼特",
                    "2024年3月",
                    "23",
                    ""
                ))

                mAnimalResponse.insert(AnimalBean(
                    245,
                    "2024年1月",
                    "苏尼特",
                    "母",
                    "47",
                    "苏尼特",
                    "2024年2月",
                    "23",
                    ""
                ))

                mAnimalResponse.insert(AnimalBean(
                    283,
                    "2024年9月",
                    "苏尼特",
                    "公",
                    "69",
                    "苏尼特",
                    "2024年11月",
                    "23",
                    ""
                ))
            }
        }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        val index = intent?.getIntentInt(JumpConstants.HOME_INDEX) ?: -1
        if (index != -1) {
            if (index == 0 || index == 1) {
                binding.MainPager.currentItem = index
            } else if (index == 2) {
                DisplayUtils.toCommunity(this@MainActivity)
            } else {
                DisplayUtils.toMine(this@MainActivity)
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ActManger.appExit()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onScroll(flag: Boolean) {
        includeBinding.root.isVisi(flag)
        binding.MainGroup.isVisi(flag.not())
    }

    override fun openMenu() {
        binding.root.open()
    }

    override fun isNeedAllTab() = true
}